<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label" />
        <el-input
          v-model="query.memberName"
          clearable
          placeholder="會員名稱"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label" />
        <el-input
          v-model="query.phone"
          clearable
          placeholder="手机号码"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" inline>
          <el-col :span="12">
            <el-form-item label="姓">
              <el-input v-model="form.firstName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名">
              <el-input v-model="form.lastName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信">
              <el-input v-model="form.wechatId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生日">
              <el-date-picker
                v-model="form.birthday"
                placeholder="選擇日期"
                type="date"
                :default-time="form.birthday"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手機號碼">
              <el-input v-model="form.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他联络號碼">
              <el-input v-model="form.otherPhone" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="會員等級">
              <el-select
                v-model="form.memberLevelId"
                placeholder="请选择會員等級"
              >
                <el-option
                  v-for="item in memberLevelList"
                  :key="item.id"
                  :label="item.nameCn"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="聯絡方式偏好">
              <el-select
                v-model="form.preferredContactWay"
                placeholder="聯絡方式偏好"
                clearable
              >
                <el-option
                  v-for="(item, index) in preferredContactWayList"
                  :key="index"
                  :label="item.value"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通知語言偏好">
              <el-select
                v-model="form.preferredLanguage"
                placeholder="通知語言偏好"
                clearable
              >
                <el-option
                  v-for="(item, index) in preferredLanguageList"
                  :key="index"
                  :label="item.value"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户来源">
              <el-select
                v-model="form.sourceType"
                placeholder="用户来源"
                clearable
              >
                <el-option
                  v-for="item in dict.sourceType"
                  :key="item.id"
                  :label="item.dictValue"
                  :value="item.dictKey"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性別">
              <el-radio v-for="item in dict.gender" :key="item.id" v-model="form.gender" :label="item.dictKey">
                {{ item.dictValue }}
              </el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="賬號是否啓用">
              <el-radio v-for="item in dict.true_false" :key="item.id" v-model="form.status" :label="item.dictKey">
                {{ item.dictValue }}
              </el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="推薦人電話號">
              <el-input v-model="form.referredFriend" />
            </el-form-item>
          </el-col>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">確認</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        row-key="id"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
        @sort-change="sortChange"
      >
        <el-table-column prop="memberName" label="會員名稱" align="right" sortable>
          <template slot-scope="scope">
            <div class="productInfo">
              <img :src="baseApi + scope.row.avatar" class="tableImg" alt="">
              <span>{{ scope.row.memberName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手機號碼" align="right" sortable />
        <el-table-column prop="memberLevel.nameCn" label="會員等級" sortable />
        <el-table-column prop="sourceType" label="用戶來源" sortable>
          <template slot-scope="scope">
            {{ dict.label.sourceType[scope.row.sourceType] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="賬號是否啓用" sortable>
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.sysStatus"
              :row-data="scope.row.status"
              row-filed="status"
              to-true-character="啓用"
              to-false-character="禁用"
              title="導航欄"
            />
          </template>
        </el-table-column>
        <el-table-column prop="gender" label="性别" align="center" sortable>
          <template slot-scope="scope">
            {{ dict.label.gender[scope.row.gender] }}
          </template>
        </el-table-column>
        <el-table-column prop="preferredLanguage" label="語言偏好" sortable />
        <el-table-column prop="createdAt" label="注冊時間" sortable />
        <el-table-column label="用戶詳情">
          <template slot-scope="scope">
            <el-button type="primary" @click="getMember(scope.row.id)">會員詳情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMember from '@/api/ums/member'
import { all } from '@/api/ums/memberLevel'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import mallSwitch from '@/components/mallSwitch'
import { mapGetters } from 'vuex'

const defaultForm = {
  memberName: null,
  firstName: null,
  lastName: null,
  birthday: null,
  gender: 0,
  memberLevelId: null,
  phone: null,
  otherPhone: null,
  sourceType: 0,
  status: false,
  wechatId: null,
  referredFriend: null,
  preferredLanguage: null,
  preferredContactWay: null,
  username: null
}
export default {
  name: 'Member',
  components: { pagination, crudOperation, rrOperation, mallSwitch },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['gender', 'sourceType', 'true_false', 'sysStatus'],
  cruds() {
    return CRUD({
      title: '會員管理', url: 'member/page', sort: 'id:desc', crudMethod: { ...crudMember }, optShow: {
        add: true,
        edit: false,
        del: false,
        download: true,
        reset: true
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'member:add'],
        edit: ['admin', 'member:edit'],
        del: ['admin', 'member:del']
      },
      rules: {},
      memberLevelList: [],
      queryTypeOptions: [
        { key: 'memberName', display_name: '用户名' },
        { key: 'nickname', display_name: '昵称' },
        { key: 'phone', display_name: '手机号码' }
      ],
      preferredLanguageList: [
        { value: '中文', label: 'Chinese' },
        { value: '英文', label: 'English' }
      ],
      preferredContactWayList: [
        { value: '电话', label: 'Phone' },
        { value: '短信', label: 'Text Message' },
        { value: '聊天APP', label: 'Social Media' }
      ]
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  created() {
    this.getMemberLevel()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getMember(id) {
      this.$router.push({ path: 'memberDetail/' + id, query: { id: id }})
    },
    getMemberLevel() {
      all().then(res => {
        if (res.code === 0) {
          this.memberLevelList = res.data
        }
      })
    },
    sortChange(column) {
      const sortColumn = column.prop
      if (column.order === 'ascending') {
        this.crud.sort = [sortColumn + ':asc']
      }
      if (column.order === 'descending') {
        this.crud.sort = [sortColumn + ':desc']
      }
      this.crud.refresh()
    }
  }
}
</script>

<style scoped>
.productInfo span {
  display: block;
  line-height: 16px;
}

.tableImg {
  width: 30px;
  margin-right: 0px;
  float: left;
}
</style>
